<template>
  <div>
    <el-row style="margin: 18px 0px 0px 18px ">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/admin/dashboard' }"
          >管理中心</el-breadcrumb-item
        >
        <el-breadcrumb-item>内容管理</el-breadcrumb-item>
        <el-breadcrumb-item>文章管理</el-breadcrumb-item>
      </el-breadcrumb>
    </el-row>
    <el-link
      href="/admin/content/editor"
      :underline="false"
      target="_blank"
      class="add-link"
    >
      <el-button type="success">写文章</el-button>
    </el-link>
    <el-card style="margin: 18px 2%;width: 95%">
      <el-table
        v-loading="loading"
        :data="articles.slice((currentPage-1)*pageSize,currentPage*pageSize)"
        stripe
        style="width: 100%"
        :max-height="tableHeight"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-form label-position="left" inline>
              <el-form-item>
                <span>{{ props.row.articleAbstract }}</span>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column prop="articleTitle" label="题目（展开查看摘要）" fit>
        </el-table-column>
        <el-table-column prop="articleDate" label="发布日期" width="200">
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="180">
          <template slot-scope="scope">
            <el-button
              @click.native.prevent="viewArticle(scope.row.id)"
              type="text"
              size="small"
            >
              查看
            </el-button>
            <el-button
              @click.native.prevent="editArticle(scope.row)"
              type="text"
              size="small"
            >
              编辑
            </el-button>
            <el-button
              @click.native.prevent="deleteArticle(scope.row.id)"
              type="text"
              size="small"
            >
              移除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="margin: 20px 0 50px 0">
        <el-pagination
          style="float:center;"
          layout="total, prev, pager, next, jumper"
          @current-change="handleCurrentChange"
          :page-size="pageSize"
          :current-page="currentPage"
          :total="total"
        >
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "ArticleManagement",
  data() {
    return {
      articles: [],
      pageSize: 10,
      currentPage: 1,
      total: 0,
      loading: true
    };
  },
  mounted() {
    this.loadArticles();
  },
  computed: {
    tableHeight() {
      return window.innerHeight - 320;
    }
  },
  methods: {
    loadArticles() {
      this.loading=true
      var _this = this;
      this.$axios.get("/article/findAll").then(resp => {
        if (resp && resp.data.code === 200) {
          _this.articles = resp.data.result
          _this.total = resp.data.result.length
          this.loading=false
        }
      });
    },
    handleCurrentChange: function (currentPage) {
        this.currentPage = currentPage
      },
    viewArticle(id) {
      let articleUrl = this.$router.resolve({
        path: "../../jotter/article",
        query: {
          id: id
        }
      });
      window.open(articleUrl.href, "_blank");
    },
    editArticle(article) {
      this.$router.push({
        name: "Editor",
        params: {
          article: article
        }
      });
    },
    deleteArticle(id) {
      this.$confirm("此操作将永久删除该文章, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$loading({
            lock: true,
            text: "Loading",
            spinner: "el-icon-loading",
            background: "rgba(0, 0, 0, 0.7)"
          });
          this.$axios.delete("/admin/content/article/" + id).then(resp => {
            if (resp && resp.data.code === 200) {
              this.$loading({
                lock: false,
                text: "Loading",
                spinner: "el-icon-loading",
                background: "rgba(0, 0, 0, 0.7)"
              }).close();
              this.$message.success(resp.data.message);
              this.loadArticles();
            } else {
              this.$loading({
                lock: false,
                text: "Loading",
                spinner: "el-icon-loading",
                background: "rgba(0, 0, 0, 0.7)"
              }).close();
              this.$message.success(resp.data.message);
              this.loadArticles();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    }
  }
};
</script>

<style scoped>
.add-link {
  margin: 18px 0 15px 10px;
  float: left;
}
</style>
